 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.edu.agh.student.logic.webapp;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;
import org.jbpm.api.IdentityService;
import pl.edu.agh.student.logic.service.RoleServiceImpl;
import pl.edu.agh.student.logic.service.UserServiceImpl;
import pl.edu.agh.student.logic.util.ServiceProvider;
import pl.edu.agh.student.model.Role;
import pl.edu.agh.student.model.User;

/**
 *
 * @author janburkot
 */
@Stateful
@Name("userBean")
@Scope(ScopeType.CONVERSATION)
@Transactional
public class UserBeanImpl implements UserBean,Serializable{

    @In(value="userService",create=true,required=false)
    @Out
    protected UserServiceImpl userService;
    @In
    Identity identity;
    @In
    Conversation conversation;
    
    @In(required = false)
    @Out(required = false)
    private User user;
    
    @In(required = false)
    @Out(required = false)
    private String password;
    
    @In(required = false)
    @Out(required = false)
    private String confirm;
    
    @Logger
	protected Log log;
    
    @In(value = "roleService", create = true, required = false)
    @Out
    protected RoleServiceImpl roleService;
    
    @In("serviceProvider")
    protected ServiceProvider serviceProvider;
    
    public String save(User user, String roleName){
        if("".equals(user.getPassword()) || !password.equals(confirm)){
            return "failure";
        }
        
        Role role = roleService.findRoleByName(roleName);
        
        user.setPassword(password);
        user.setEnabled(true);
        userService.saveOrUpdate(user);
        
        user.getRoles().add(role);
        role.getUsers().add(user);
        userService.saveOrUpdate(user);
        roleService.saveOrUpdate(role);
        
        IdentityService identityService = serviceProvider.getIdentityService();
        identityService.createUser(user.getUsername(), user.getUsername(), user.getEmail());
        
        if(getJbpmGroup(roleName) != null){
            identityService.createMembership(user.getUsername(), getJbpmGroup(roleName));
        }
        
        conversation.end();
        return "success";
    }
    
    public String delete(User user){
        user.setEnabled(false);
        userService.saveOrUpdate(user);
        return "success";
    }
    
    public List<User> getAll(){
        return userService.getAll();
//        System.out.println(identity.hasPermission("seam.user", "read"));
//        if(identity.hasRole("Boss")){
//            return userService.getAll();
//        }
//        else if(identity.hasRole("Klinet")){
//            User user = userService.getDao().findByProperty("username", identity.getPrincipal().getName()).get(0);
//            return userService.getDao().findByProperty("person", user.getPerson());
//        }
//        else{
//            return null;
//        }
    }
    
    private String getJbpmGroup(String role){
        if("Szef".equals(role)){
            return "boss-group";
        }
        else if("Analityk".equals(role)){
            return "analyst-group";
        }
        else if("Klient".equals(role)){
            return "customer-group";
        }
        else{
            return null;
        }
        
    }

    public User getSelectedUser() {
        return user;
    }

    public void setSelectedUser(User user) {
        this.user = user;
    }

    public void refresh(User user) {
        userService.refresh(user);
    }
    
    @Destroy
    @Remove
    public void destroy() {
	log.debug("destry component #0", this);
    }

    public void addRole() {
        System.out.println("addRole");
    }

    public void createUser() {
        conversation.begin();
    }

    public List<String> getRoles() {
        List<Role> roles = roleService.getAll();
        List<String> t= new LinkedList<String>();
        
        for(Role r: roles){
            t.add(r.getName());
        }
        return t;
    }
    
    public User getCurrentUser(){
        User _user = userService.findByUsername(identity.getPrincipal().getName());
        return _user;
    }
}
